package com.bdh.demo.controller;

import com.bdh.demo.service.HomeService;
import com.bdh.demo.vo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by baodehua on 2016/11/23.
 */
@Controller
@RequestMapping("/home")
public class HomeController {

    @Autowired
    private HomeService homeService;

    @RequestMapping(value = "/hello")
    public ModelAndView sayHello(String message) {
        if (StringUtils.isEmpty(message)) {
            message = "Hello Spring MVC Framework!";
        }


        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("message", message);
        homeService.hello(message);
        return mv;
    }

    @RequestMapping(value = "/getCommentList")
    @ResponseBody
    public List<Comment> getCommentList() {
        List<Comment> commentList = new ArrayList<Comment>();

        Comment comment1 = new Comment();
        comment1.setAuthor("Pete Hunt");
        comment1.setText("This is one comment");
        commentList.add(comment1);

        Comment comment2 = new Comment();
        comment2.setAuthor("Jordan Walke");
        comment2.setText("This is *another* comment");
        commentList.add(comment2);

        Comment comment3 = new Comment();
        comment3.setAuthor("Yu Lao");
        comment3.setText("This is third comment!");
        commentList.add(comment3);

        return commentList;
    }
}
